package com.nulp.asu.generator.additional;

import org.docx4j.Docx4J;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.File;
/**
 * Created by XemyL on 23.11.2014.
 */
public abstract class DocxToXmlConverter {
    public static void convertDocxToXml(String inputFile, String outputFile) {
        // Do we want to save output?
        boolean save = true;
        // If so, whereto?

        // Open a document from the file system
        WordprocessingMLPackage wmlPackage = null;
        try {
            wmlPackage = Docx4J.load(new java.io.File(inputFile));
        } catch (Docx4JException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (save) {
            try {
                Docx4J.save(wmlPackage, new File(outputFile),
                        Docx4J.FLAG_SAVE_FLAT_XML);
            } catch (Docx4JException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Saved: " + outputFile);
        } else {
            try {
                Docx4J.save(wmlPackage, System.out, Docx4J.FLAG_SAVE_FLAT_XML);
            } catch (Docx4JException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
